//
//  ViewController.swift
//  4-5classWork
//
//  Created by Qingke on 16/4/5.
//  Copyright © 2016年 Qingke. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    //MARK: propert
   
    @IBOutlet weak var ranting: RatingControl!
    @IBOutlet weak var inputTextfield: UITextField!
    @IBOutlet weak var selectImg: UIImageView!
    
    @IBOutlet weak var saveButton: UIBarButtonItem!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        inputTextfield.delegate = self
        checkSaveButton()
        if let selectBeauty = beauty {
            inputTextfield.text = selectBeauty.name
            selectImg.image = selectBeauty.photo
            ranting.rating = Int(selectBeauty.ranting)
        }
        
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    //MARK: Action
    
    @IBAction func cancelButtonAction(sender: UIBarButtonItem) {
        dismissViewControllerAnimated(true, completion: nil)
        print("dismiss")
        
        let isAddMode = presentingViewController is UINavigationController
        if isAddMode {
            dismissViewControllerAnimated(true, completion: nil)
        } else {
            navigationController?.popViewControllerAnimated(true)
        }
    }
    @IBAction func button(sender: UIButton) {
       
    }
    
    
    @IBAction func selectGesture(sender: UITapGestureRecognizer) {
        inputTextfield.resignFirstResponder()
        let piceImage = UIImagePickerController()
        piceImage.sourceType = .PhotoLibrary
        piceImage.delegate = self
        presentViewController(piceImage, animated: true, completion: nil)
    }
    var beauty:Beauty?
    
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if saveButton === sender {
            let name = inputTextfield.text ?? ""
            let photo = selectImg.image
            let rating = ranting.rating
            
            beauty = Beauty(name: name, photo: photo, ranting: Double( rating))
        }
    }
    
    func checkSaveButton() {
        if let text = inputTextfield.text{
            saveButton.enabled = !(text.isEmpty)
        }
    }
    

    
}

extension ViewController : UITextFieldDelegate {
    func textFieldShouldReturn(textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
        
    }
    
    func textFieldDidEndEditing(textField: UITextField) {
        navigationItem.title = inputTextfield.text
        checkSaveButton()
        
    }
    
    func textFieldDidBeginEditing(textField: UITextField) {
        saveButton.enabled = false
    }
    
    
}
    
extension ViewController: UIImagePickerControllerDelegate {
    func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
        
        selectImg.image = image
        dismissViewControllerAnimated(true, completion: nil)
        
    }
    func imagePickerControllerDidCancel(picker: UIImagePickerController) {
        
        dismissViewControllerAnimated(true, completion: nil)
    }
}
//extension 不能写在类内，需要注意


extension ViewController :UINavigationControllerDelegate {
    
}
